export default {
  install(Vue) {
    const fontSizeDefault = 100
    const docEl = document.documentElement

    // adjust body font size
    function setBodyFontSize() {
      if (document.body) {
        document.body.style.fontSize = 16 + 'px'
      } else {
        document.addEventListener('DOMContentLoaded', setBodyFontSize)
      }
    }
    setBodyFontSize()

    // set 1rem = viewWidth / 10
    function setRemUnit() {
      var rem = (docEl.clientWidth / 375) * fontSizeDefault
      docEl.style.fontSize = rem + 'px'
    }
    setRemUnit()

    // reset rem unit on page resize
    window.addEventListener('resize', setRemUnit)
    window.addEventListener('pageshow', function(e) {
      if (e.persisted) {
        setRemUnit()
      }
    })
  }
}
